package com.nf.health.app.activity.global;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nf.health.app.R;
import com.nf.health.app.login.activity.LoginActivity;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;

import cn.jpush.android.api.JPushInterface;

/**
 * @ClassName: SplashActivity
 * @Description:侧滑欢迎页
 * @author Ian
 * @date 2015-11-4 上午11:05:26
 *
 */
public class SplashActivity extends Activity {

	private static final int sleepTime = 1000;
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if (!PreferenceHelper.getBoolean("IS_FIRST_TIME", false)) {
				ActivityUtils.skipActivity(SplashActivity.this, WelcomeActivity.class);
			}else {
				if(TextUtils.isEmpty(PreferenceHelper.getString(PreferenceHelper.USERNAME, "")) || !Util.isLogin()){
					ActivityUtils.skipActivity(SplashActivity.this, LoginActivity.class);
				}else{
					ActivityUtils.skipActivity(SplashActivity.this, MainActivity.class);
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ImageView iv = new ImageView(this);
		iv.setBackgroundResource(R.drawable.bk_splash);
		iv.setScaleType(ScaleType.FIT_XY);
		iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		setContentView(iv);
	}

	@Override
	protected void onStart() {
		super.onStart();

		new Thread(new Runnable() {
			public void run() {
//				if (EMClient.getInstance().isLoggedInBefore()) {
////					 ** 免登陆情况 加载所有本地群和会话
////					不是必须的，不加sdk也会自动异步去加载(不会重复加载)；
////					加上的话保证进了主页面会话和群组都已经load完毕
					long start = System.currentTimeMillis();
//					EMClient.getInstance().groupManager().loadAllGroups();
//					EMClient.getInstance().chatManager().loadAllConversations();
					long costTime = System.currentTimeMillis() - start;
//					//等待sleeptime时长
					if (sleepTime - costTime > 0) {
						try {
							Thread.sleep(sleepTime - costTime);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					mHandler.sendEmptyMessage(100);
//					进入主页面
//					startActivity(new Intent(SplashActivity.this, MainActivity.class));
//					finish();
//				}else {
//					try {
//						Thread.sleep(sleepTime);
//					} catch (InterruptedException e) {
//					}
////					startActivity(new Intent(SplashActivity.this, LoginActivity.class));
////					finish();
//					mHandler.sendEmptyMessage(100);
//				}
			}
		}).start();
	}
	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
	}


}
